/**
 * 子渠道管理初始化
 */
var ChannelSon = {
    id: "ChannelSonTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
ChannelSon.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '渠道', field: 'channel', visible: true, align: 'center', valign: 'middle'},
            {title: '是否使用', field: 'isUse', visible: true, align: 'center', valign: 'middle',formatter:isUseFormatter},
            {title: 'jar包', field: 'jarId', visible: true, align: 'center', valign: 'middle'},
            {title: '时间', field: 'date', visible: true, align: 'center', valign: 'middle'},
            {title:'添加jar包',field:'id',align:'center',valign:'middel',events:operateEvents,formatter:addJar}
    ];
};

function addJar(value,row,index){
    return ['<input type="button" id="btnId" value="添加jar包" class="btn btn-primary btn-sm" />'].join("");
};

window.operateEvents= {
    'click #btnId': function (e, value, row, index) {
        var index = layer.open({
            type: 2,
            title: '添加jar包',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/channelSon/channelSon_addJar/'+row.channel
        });
        this.layerIndex = index;
    }
}

function isUseFormatter(value){
    return value=="1"?"使用":"停用";
};

/**
 * 检查是否选中
 */
ChannelSon.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        ChannelSon.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加子渠道
 */
ChannelSon.openAddChannelSon = function () {
    var index = layer.open({
        type: 2,
        title: '添加子渠道',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/channelSon/channelSon_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看子渠道详情
 */
ChannelSon.openChannelSonDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '子渠道详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/channelSon/channelSon_update/' + ChannelSon.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除子渠道
 */
ChannelSon.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/channelSon/delete", function (data) {
            Feng.success("删除成功!");
            ChannelSon.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("channelSonId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询子渠道列表
 */
ChannelSon.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    ChannelSon.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = ChannelSon.initColumn();
    var table = new BSTable(ChannelSon.id, "/channelSon/list", defaultColunms);
    table.setPaginationType("client");
    ChannelSon.table = table.init();
});
